package com.example.boot.security.sms;

/**
 * TODO
 *
 * @author ding
 * @since 2024/7/24
 */

import cn.hutool.core.text.AntPathMatcher;
import com.example.boot.security.SecurityHandler;
import com.example.properties.JwtProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * 第一步：拦截请求，验证手机验证码是否正确
 * 关靠这个无法执行，下一步需要写入口
 */
//@Component
@Slf4j
public class SmsCodeCheckFilter extends OncePerRequestFilter {

    @Autowired
    private SecurityHandler securityHandler;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RedisTemplate redisTemplate;

    public void setSecurityHandler(SecurityHandler securityHandler) {
        this.securityHandler = securityHandler;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // 不是做短信登录，直接放行
        if(!new AntPathMatcher().match("/validSms", request.getRequestURI())) {
            // 放行
            filterChain.doFilter(request, response);
            return;
        }
        if (securityHandler != null) {

            filterChain.doFilter(request, response);
        }
    }
}
